<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class eventman extends CI_Controller 
{
    function __construct(){
            parent::__construct();
            $this->load->helper('url');
            $this->load->model('entity_model');
            $this->load->library('session');
            $this->load->helper('date');
            
            //cek session dan tipe member untuk sekuritas web
            if (!$this->session->userdata('username')|| $this->session->userdata('tipemember') !=2)
            {
                redirect(base_url());
                return;
            }
    }
    
//    ===============================================
//    FUNGSI PEMANGGILAN VIEW WEB
//    ===============================================
    public function index()
    {
        $data['username'] = $this->session->userdata('username');
        $data['title'] = 'Event Management';
        $data['subtitle'] = '- Home';
        $this->load->view('_header',$data);
        $this->load->view('event/event_view',$data);
        $this->load->view('_footer',$data);   
    }
    
    public function items()
    {
        $datestring = "%D, %d-%M-%Y (%h:%i:%s %A)";
        $datestringSQL = "%Y-%m-%d(%h:%i:%s)";
        $totaldate= mdate($datestring, gmt_to_local(time(),'UM7',FALSE));
        $sql = mdate($datestringSQL,gmt_to_local(time(),'UM7',FALSE));
        
        $data['username'] = $this->session->userdata('username');
        $data['title'] = 'Event Management';
        $data['subtitle'] = '- Add Items';
        $data['time'] = $totaldate;
        $data['sqltime'] = $sql;
        $data['nameDepart'] = $this->showNameDepart($this->session->userdata('username'));
        
        $data['idDepart'] = $this->showIDDepart($this->session->userdata('username'));
        
        $this->load->view('_header',$data);
        $this->load->view('event/items',$data);
        $this->load->view('_footer',$data);
    } 
    
    public function search()
    {
        $data['username'] = $this->session->userdata('username');
        $data['title'] = 'Event Management';
        $data['subtitle'] = '- Search Items';
        $this->load->view('_header',$data);
        $this->load->view('event/search',$data);
        $this->load->view('_footer',$data);
    } 
    
    public function showIDDepart($uname)
    {
        $q = $this->entity_model->getDepartment($uname);
        if ($q->num_rows() <=0)
        {
            redirect(base_url().'index.php/eventman/');
        }
        else $hasil = $q->row();
        
        return $hasil->DEPART_ID; 
    }
    public function showNameDepart($uname)
    {
        $q = $this->entity_model->getDepartment($uname);
        if ($q->num_rows() <=0)
        {
            redirect(base_url().'index.php/eventman/');
        }
        else $hasil = $q->row();
        
        $q1 = $this->entity_model->getNameDepartment($hasil->DEPART_ID);
        if ($q1->num_rows() <=0)
        {
            redirect(base_url().'index.php/eventman/');
        }
        else $hasil1 = $q1->row();
        
        return $hasil1->DEPART_NAME;
               
    }
    
    public function controllerView()
    {
        $depart = $this->input->post('depart_value');
        $time =$this->input->post('time_value'); 
        $tipe= $this->input->post('tipe'); 
        $username = $this->input->post('name_value');
        
        if ($tipe == 'lecture')
        {
            $html_result = 
            '
             <script>
                 datepicker();
             </script>
             
                <div class="content-form">
                <form id="form2" method="post" action=" '.base_url().'index.php/eventman/sendLecture'.'" >
                    <table>
                        <tr>
                            <td>Sending / Inviting</td>
                            <td><select name="tipe" class="txt_submit">
                                  <option value="0">Sending</option>
                                  <option value="1">Inviting</option>
                                </select>
                            </td>
                        </tr>
                       <tr>
                            <td>Lecturer Name</td>
                            <td><input class="txt_submit" id="input_ln" type="text" name="box_ln" value="" /> </td>
                        </tr>
                        <tr>
                            <td>Affiliation </td>
                            <td><input class="txt_submit" id="input_af" type="text" name="box_af" value="" /> </td>
                        </tr>
                        <tr>
                            <td>Place of Lecture</td>
                            <td><input class="txt_submit" id="input_pl" type="text" name="box_pl" value="" /> </td>
                        </tr>
                        <tr>
                            <td>Subject of Lecture</td>
                            <td><input class="txt_submit" id="input_sl" type="text" name="box_sl" value="" /> </td>
                        </tr>
                        <tr>
                            <td>Date of Lecture</td>
                            <td>
                                <input class="txt_submit" type="text" id="datepicker" name="box_tm" readonly/>
                            </td>
                        </tr>
                    </table>
                    <input type="hidden" name="time_value" value="'.$time.'"/>
                    <input type="hidden" name="depart_value" value="'.$username.'" />
                </form>
                

                <div id="ajax_result_form"></div>   

                </div>

                <div class="button_form" onclick="ajax_formSubmit();">Submit</div>
            ';
        }
        else if ($tipe == 'exchange')
        {
            $html_result = 
            '
                <div class="content-form">
                <form id="form3" method="post" action=" '.base_url().'index.php/eventman/sendExchange'.'" >
                    <table>
                        <tr>
                            <td width = "45%" >Sending / Intake</td>
                            <td>
                                <select name="tipe" class="txt_submit">
                                  <option value="0">Sending</option>
                                  <option value="1">Intake</option>
                                </select>
                            </td>
                        </tr>
                       <tr>
                            <td>Student Name</td>
                            <td><input class="txt_submit" id="input_st" type="text" name="student" value="" /> </td>
                        </tr>
                        <tr>
                            <td>Student University</td>
                            <td><input class="txt_submit" id="input_un" type="text" name="univ" value="" /> </td>
                        </tr>
                        <tr>
                            <td>Country of University</td>
                            <td><input class="txt_submit" id="input_cn" type="text" name="country" value="" /> </td>
                        </tr>
                        <tr>
                            <td>Start - Year </td>
                            <td>
                                <input class="txt_submit" id="input_sy" type="text" name="syear" value="" /> 
                            </td>
                        </tr>
                            <tr>
                            <td>Start - Semester</td>
                            <td>
                                <select name="ssem" class="txt_submit">
                                  <option value="0">Spring</option>
                                  <option value="1">Fall</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>End - Year </td>
                            <td>
                                <input class="txt_submit" id="input_ey" type="text" name="endyear" value="" /> 
                            </td>
                        </tr>
                        <tr>
                            <td>End - Semester</td>
                            <td>
                                <select name="endsem" class="txt_submit">
                                  <option value="0">Spring</option>
                                  <option value="1">Fall</option>
                                </select>

                            </td>
                        </tr>
                        <tr>
                            <td>Type of study</td>
                            <td>
                                <select name="tipestudi" class="txt_submit">
                                  <option value="0">Undergraduate</option>
                                  <option value="1">Master</option>
                                  <option value="2">Ph.D</option>
                                  <option value="3">Post-Doc</option>
                                </select>
                            </td>
                        </tr>
                    </table>
                    <input type="hidden" name="time_value" value="'.$time.'"/>
                    <input type="hidden" name="depart_value" value="'.$username.'" />
                </form>
                

                <div id="ajax_result_form_ex"></div>   

                </div>

                <div class="button_form_ex" onclick="ajax_formSubmitEx();">Submit</div>
            ';
        }
        else if ($tipe == 'agreement')
        {
            $html_result = 
            '
             <script>
                 datepicker();
             </script>
             
                <div class="content-form">
                <form id="form4" method="post" action=" '.base_url().'index.php/eventman/sendAgree'.'" >
                    <table>
                        <tr>
                            <td>Event Type</td>
                            <td><select name="tipe" class="txt_submit">
                                  <option value="0">Conference</option>
                                  <option value="1">Summit</option>
                                  <option value="2">Festival</option>
                                  <option value="3">Workshop</option>
                                  <option value="4">Exhibition</option>
                                  <option value="5">Other</option>
                                </select>
                            </td>
                        </tr>
                       <tr>
                            <td>Event Name</td>
                            <td><input class="txt_submit" id="input_en" type="text" name="box_en" value="" /> </td>
                        </tr>
                        <tr>
                            <td>Date of Event</td>
                            <td><input class="txt_submit" type="text" id="datepicker" name="box_tm" readonly/> </td>
                        </tr>
                        <tr>
                            <td>International Parterns</td>
                            <td><input class="txt_submit" id="input_ip" type="text" name="box_ip" value="" /> </td>
                        </tr>
                        <tr>
                            <td>Number of participants</td>
                            <td><input class="txt_submit" id="input_np" type="text" name="box_np" value="" /> </td>
                        </tr>
                        <tr>
                            <td>Countries Involved</td>
                            <td>
                                <input class="txt_submit" id="input_ci" type="text" name="box_ci" value="" />
                            </td>
                        </tr>
                    </table>
                    <input type="hidden" name="time_value" value="'.$time.'"/>
                    <input type="hidden" name="depart_value" value="'.$username.'" />
                </form>
                

                <div id="ajax_result_form_ag"></div>   

                </div>

                <div class="button_form" onclick="ajax_formSubmitAg();">Submit</div>
            ';
        }
        else if ($tipe == 'event')
        {
            $html_result = $tipe;
        }
        else if ($tipe == 'research')
        {
            $html_result = $tipe;
        }
        else if ($tipe=='0')
        {
            $html_result=' <div class="content-form">
                
                </div>';
        }

        echo json_encode(array('renderhtml'=>$html_result));
        exit;
    }
    
    public function sendLecture()
    {
        $tipe = $this->input->post('tipe');
        $username = $this->input->post('depart_value');
        $lecName = $this->input->post('box_ln');
        $aff = $this->input->post('box_af');
        $place = $this->input->post('box_pl');
        $subject = $this->input->post('box_sl');
        $date = $this->input->post('box_tm');
        $timeSubmit = $this->input->post('time_value');
        
        
        
        if ($username && $timeSubmit && $lecName && $aff && $subject && $date)
        {
            $result = $this->entity_model->setLecture($username,$timeSubmit,$tipe,$lecName,$aff,$place,$subject,$date);
        }
        else $result = 'Please complete this Form!';
        
        echo json_encode(array('renderhtml'=>$result));
    }
    public function sendExchange()
    {
        $tipe = $this->input->post('tipe');
        $sname = $this->input->post('student');
        $univ = $this->input->post('univ');
        $country = $this->input->post('country');
        $starty = $this->input->post('syear');
        $starts = $this->input->post('ssem');
        $endy = $this->input->post('endyear');
        $ends = $this->input->post('endsem');
        $tstudy = $this->input->post('tipestudi');
        $timeSubmit = $this->input->post('time_value');
        $username = $this->input->post('depart_value');
        
        if ($sname && $univ && $country && $starty && $endy)
        {
            $result = $this->entity_model->setExchange($username, $timeSubmit, $tipe, $sname, $univ, $country,
                $starty,$starts,$endy, $ends, $tstudy);
        }
        else
            $result = 'Please complete this Form!';
        //$result = $username.$timeSubmit.$tipe.$sname.$univ.$country.$starty.$starts.$endy.$ends.$tstudy;
        echo json_encode(array('renderhtml'=>$result));
    }
    public function sendAgree()
    {
        $tglSubmit = $this->input->post('time_value');
        $username = $this->input->post('depart_value');
        $tipe = $this->input->post('txt_submit');
        $eventname = $this->input->post('box_en');
        $adate = $this->input->post('box_tm');
        $partner = $this->input->post('box_ip');
        $participant = $this->input->post('box_np');
        $country = $this->input->post('box_ci');
        if ($username && $tglSubmit && $eventname && $adate && $partner && $participant && $country)
        {
            $result = $this->entity_model->setAgree($username,$tglSubmit,$tipe,$eventname,$adate,$partner,$participant,$country);
        }
        else
            $result = 'Please complete this Form!';
        //$result = $username.$timeSubmit.$tipe.$sname.$univ.$country.$starty.$starts.$endy.$ends.$tstudy;
        echo json_encode(array('renderhtml'=>$result));
    }
    
    
}

?>
